package com.xutong.myfunction.custom;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.widget.Button;
import android.widget.TextView;

/**
 * Created by Administrator on 2016/12/22.
 */
public class CustomButton extends TextView {

    Paint paint = new Paint();
    public CustomButton(Context context) {
        super(context);
    }

    public CustomButton(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onDraw(Canvas canvas) {

        int width = getWidth();
        int height =getHeight();
        Bitmap bitmap = Bitmap.createBitmap(width,height, Bitmap.Config.ARGB_8888);

        int bw = bitmap.getWidth();
        int bh = bitmap.getHeight();

        paint.setColor(Color.WHITE);
        paint.setAntiAlias(true);
        paint.setStyle(Paint.Style.FILL);

        RectF rectF = new RectF(0,0,bw,bh/5*4);
        canvas.drawRoundRect(rectF,20,20,paint);

        int lengthW = bw/10;
        int lengthH = bh/5;

        Path path = new Path();
        path.moveTo(lengthW*8,lengthH*4);
        path.lineTo(lengthW*8+lengthW/2,bh);
        path.lineTo(lengthW*9,lengthH*4);
        path.close();
        canvas.drawPath(path,paint);

        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
        Rect rect = new Rect(0,0,bw,bh);
        paint.reset();
        canvas.drawBitmap(bitmap,rect,rect,paint);

        super.onDraw(canvas);

    }


    public void startAnimation(){
            if (this.getVisibility() == VISIBLE){
                AnimationSet animationSet2 = new AnimationSet(true);
                AlphaAnimation alphaAnimation2 = new AlphaAnimation(1,0);
                alphaAnimation2.setAnimationListener(new Animation.AnimationListener() {
                    @Override
                    public void onAnimationStart(Animation animation) {

                    }

                    @Override
                    public void onAnimationEnd(Animation animation) {
                        setVisibility(View.INVISIBLE);
                    }

                    @Override
                    public void onAnimationRepeat(Animation animation) {

                    }
                });
                alphaAnimation2.setDuration(1000);
                animationSet2.addAnimation(alphaAnimation2);
                this.startAnimation(animationSet2);
            }else {
                AnimationSet animationSet = new AnimationSet(true);
                AlphaAnimation alphaAnimation = new AlphaAnimation(0,1);
                alphaAnimation.setAnimationListener(new Animation.AnimationListener() {
                    @Override
                    public void onAnimationStart(Animation animation) {

                    }

                    @Override
                    public void onAnimationEnd(Animation animation) {
                        setVisibility(View.VISIBLE);
                    }

                    @Override
                    public void onAnimationRepeat(Animation animation) {

                    }
                });
                alphaAnimation.setDuration(1000);
                animationSet.addAnimation(alphaAnimation);
                this.startAnimation(animationSet);
            }
    }


}
